package com.yunwushanhe.controller;

import com.yunwushanhe.bean.ret.ResponseResult;
import com.yunwushanhe.bean.vo.ArticleDetailVO;
import com.yunwushanhe.bean.vo.ArticleVO;
import com.yunwushanhe.bean.vo.ArticleHotVO;
import com.yunwushanhe.bean.vo.PageVO;
import com.yunwushanhe.entity.Article;
import com.yunwushanhe.service.ArticleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文章表(Article)表控制层
 *
 * @author makejava
 * @since 2023-08-04 12:45:36
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @GetMapping("/list")
    public List<Article> test(){
        return articleService.list();
    }

    @GetMapping("/hotArticleList")
    public ResponseResult<List<ArticleHotVO>> hotArticleList(){
        return articleService.hotArticleList();
    }

    @GetMapping("/articleList")
    public ResponseResult<PageVO<List<ArticleVO>>> articleList(Integer pageNum,
                                                               Integer pageSize,
                                                               Long categoryId){
        return articleService.articleList(pageNum,pageSize,categoryId);
    }

    @GetMapping("/{id}")
    public ResponseResult<ArticleDetailVO> getArticleDetail(@PathVariable("id") Long id){
        return articleService.getArticleDetail(id);
    }

    // 这个方法可以优化一下，加上像mysql那样的事务
    @PutMapping("/updateViewCount/{id}") // 这请求每次访问到文章就会自动触发去更新redis中的浏览量
    public ResponseResult<Object> updateViewCount(@PathVariable("id") Long id){
        return articleService.updateViewCount(id);
    }

}

